'''
Created on Jan 3, 2012

FatSuit Rig Utilities v_0.1

@author: Christoph Westphal
'''

import maya.cmds as cmds

global stateFilePath
global settings

stateFilePath = "C:/Users/Me/git./nerdy-playground/Python Development/Maya/bin/"
#stateFilePath = "P:/10_script/fatSuit/fatPose/"


align = True
settings = False

    
def getNamespace (obj):
    
    if ':' in obj:
    
        ns = obj[:obj.index(':')]
        return ns
    
    else:
        
        return ''


def getName (obj):
    
    if ':' in obj:
        
        n = obj[obj.index(':')+1:]
        return n
    
    else:
        
        return obj
    
    
def getPrefix (obj):
    
    if ':' in obj:
        
        n = obj[obj.index(':')+1 : obj.index(':')+4]
        return n
    
    else:
        
        n = obj[0:3]   
        return n


def getState (obj):
    
    state = {}
    
    ns = getNamespace(obj[0])
    
    for obj in obj:
        
        n = getName(obj)
        
        attr = cmds.listAttr(obj, keyable = True, unlocked = True, visible = True)
        
        for attr in attr:
            
            state['%s.%s' %(n, attr)] = cmds.getAttr('%s:%s.%s' %(ns, n, attr))
            
    return state
 
   
def addPose (poseName, sel):
    
                   
    ns = getNamespace(sel[0])
    state = getState(sel)
    
    poseDict = {}
    poseDict = readPoseDict()
        
    poseDict['%s' %poseName]  = state
    
    sFile = open('%s%s_pose.txt' %(stateFilePath, ns), 'w')
    sFile.write('%s' %poseDict)
    sFile.close()
 
 
def removePose (poseName):   

    sel = cmds.ls(selection = True)
    ns = getNamespace(sel[0])
       
    poseDict = {}
    poseDict = readPoseDict()
    
    del poseDict['%s' %poseName]
    
    sFile = open('%s%s_pose.txt' %(stateFilePath, ns), 'w')
    sFile.write('%s' %poseDict)
    sFile.close()
 
    
    
def setPose (poseName):
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])

    
    poseDict = {}    
    poseDict = readPoseDict()
    
    pose = {}    
    pose = poseDict[poseName]
    
    for attr in pose:
        
        try:
            cmds.setAttr('%s:%s' %(ns, attr), pose[attr])
        except:
            continue
    
    
def readPoseDict ():
      
    poseDict = {}

    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    
    try:
    
        sFile = open('%s%s_pose.txt' %(stateFilePath, ns), 'r')
        
    except:
        
        sFile = open('%s%s_pose.txt' %(stateFilePath, ns), 'a')
    
    try:
        
        poseDict = eval(sFile.read())
        
    finally:
    
        sFile.close()
    
        return poseDict
   

class savePoseWindow ():

     
    def savePose(self):
        
        self.name = cmds.textFieldButtonGrp("textGrp1",query=True,text=True)
        sel = cmds.ls (selection = True)
        addPose(self.name, sel)
        cmds.deleteUI('window', window = True)

    def __init__(self):
        
        if cmds.window('window', exists = True):
            cmds.deleteUI('window', window = True)
            
        window = cmds.window('window', title = 'Save Pose', widthHeight = [200,20], sizeable = False)
        cmds.columnLayout()
        self.name = ''
        cmds.textFieldButtonGrp("textGrp1", label='Save Pose as:', text = self.name, buttonLabel ='Save', buttonCommand=self.savePose)
        cmds.showWindow(window)
    '''
    def savePose(self):
        self.name = cmds.textField("textGrp1",query=True,text=True)
        sel = cmds.ls (selection = True)
        addPose(self.name, sel)
        #cmds.deleteUI('window', window = True)

    def __init__(self):
        if cmds.window('window', exists = True):
            cmds.deleteUI('window', window = True)
        window = cmds.window('window', width = 200)
        cmds.columnLayout(width = 200)
        self.name = ''
        cmds.textField("textGrp1", text = self.name, enterCommand = self.savePose(), alwaysInvokeEnterCommandOnReturn = True, width = 198)
        cmds.showWindow(window)
    '''
    
        
def switchInclSet():
    
    global settings
    
    if settings == False:
    
        settings = True
        
        print 'Include Leg Settings on'
        
    else:
        
        settings = False
        
        print 'Include Leg Settings off'
        
def alignFKshldr ():
    
    #Aligns selected FK shldr to current IK state. FK and IK should not be blended meanwhile.
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    trg = 'leg_FK_shldr_ctrl'
    src = 'leg_RESULT_jnt_1'
    attr = cmds.listAttr('%s:%s_%s' %(ns, pfx, trg),keyable = True, string = ('rotate*', 'scale*'))

    for attr in attr:
        
        cmds.setAttr('%s:%s_%s.%s' %(ns, pfx, trg, attr),cmds.getAttr('%s:%s_%s.%s' %(ns, pfx, src, attr)))


def alignFKleg (fik = False):
        
    #Aligns FK leg to FIK or IK state.
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])

    #get current FK IK State
    
    val_fkik = cmds.getAttr('%s:%s_leg_settings.FK_IK' %(ns, pfx))
    val_fik = cmds.getAttr('%s:%s_leg_IK_mov_ctrl.Full_IK' %(ns, pfx))
    
    
    #set state to IK
    
    cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns, pfx), 10)
    
    if fik == True:
        
        cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Full_IK' %(ns, pfx), 10)
        
    else:
    
        cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Full_IK' %(ns, pfx), 0)

    #copy IK values to FK
    
    alignFKblend()
    
    #reset FK IK State
    
    cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns, pfx), val_fkik)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Full_IK' %(ns, pfx), val_fik)


def alignFKblend_old ():
    
    #Aligns FK to current IK state
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    trg = ['%s:%s_leg_FK_shldr_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_upper_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_lower_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_foot_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_toe_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_toe_end_jnt' %(ns, pfx)]
    src = ['%s:%s_leg_RESULT_jnt_1' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_2' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_3' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_4' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_5' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_6' %(ns, pfx)]
    attr = cmds.listAttr('%s' %(trg[0]), keyable = True, string = ('rotate*')) 
    val ={}
    
    #get current RESULT Leg Values
    
    for i in range(len(trg)):
        
        for x in range(len(attr)):
            
            val['%s.%s' %(trg[i],attr[x])] = cmds.getAttr('%s.%s' %(src[i],attr[x]))
            
    val['%s.Leg_Stretch' %trg[0]] = cmds.getAttr('%s.scaleX' %src[1])
        
    #set FK Values
    
    for i in range(len(trg)):
        
        for x in range(len(attr)):
                
                tmp = '%s.%s' %(trg[i],attr[x])
                cmds.setAttr('%s' %tmp, val[tmp])
    
    cmds.setAttr('%s.Leg_Stretch' %trg[0],val['%s.Leg_Stretch' %trg[0]])
    
    #set FK_shldr_orient to zero
    
    n = 'leg_FK_shldr_orient_ctrl'
    
    shldr_attr = cmds.listAttr('%s:%s_%s' %(ns, pfx, n), keyable = True, string = ('rotate*'))
           
    for x in range(len(shldr_attr)):
        
        tmp = '%s:%s_%s.%s' %(ns, pfx, n, attr[x])
        cmds.setAttr('%s' %tmp, 0)
        
        
def alignFKblend ():
    
    #Aligns FK to current IK state
    
    
    #Aligns FK to current IK state
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    trg = ['%s:%s_leg_FK_shldr_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_upper_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_lower_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_foot_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_toe_ctrl' %(ns, pfx),
           '%s:%s_leg_FK_toe_end_jnt' %(ns, pfx)]
    src = ['%s:%s_leg_RESULT_jnt_1' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_2' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_3' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_4' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_5' %(ns, pfx),
           '%s:%s_leg_RESULT_jnt_6' %(ns, pfx)]
    attr = cmds.listAttr('%s' %(trg[0]), keyable = True, string = ('rotate*')) 
    val ={}
    
    #get current RESULT Leg Values
    
    for i in range(len(trg)):
        
        for iattr in attr:
            
            val['%s.%s' %(trg[i], iattr)] = cmds.getAttr('%s.%s' %(src[i], iattr))
            
    val['%s.Leg_Stretch' %trg[0]] = cmds.getAttr('%s.scaleX' %src[1])
        
    #set FK Values
    
    for i in range(len(trg)):
        
        for iattr in attr:
                
                tmp = '%s.%s' %(trg[i], iattr)
                cmds.setAttr('%s' %tmp, val[tmp])
    
    cmds.setAttr('%s.Leg_Stretch' %trg[0],val['%s.Leg_Stretch' %trg[0]])
    
    #set FK_shldr_orient to zero
    
    n = 'leg_FK_shldr_orient_ctrl'
    
    shldr_attr = cmds.listAttr('%s:%s_%s' %(ns, pfx, n), keyable = True, string = ('rotate*'))
           
    for attr in shldr_attr:
        
        tmp = '%s:%s_%s.%s' %(ns, pfx, n, attr)
        cmds.setAttr('%s' %tmp, 0)
        
        
def alignIKleg ():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    
    tmp_help = cmds.spaceLocator()
    cmds.xform( rotateOrder='zyx' )
    toe_jnt = ['%s:%s_leg_IK_switch_hlp' %(ns,pfx)]
    ik_ctrl = ['%s:%s_leg_IK_mov_ctrl' %(ns,pfx)]
    
    cmds.parent (tmp_help, toe_jnt[0])
    
    cmds.makeIdentity(tmp_help)
        
    cmds.parent( '%s|%s'%(toe_jnt[0],tmp_help[0]), world=True )
    
    X = cmds.getAttr('%s.translateX' %(tmp_help[0]))
    Y = cmds.getAttr('%s.translateY' %(tmp_help[0]))
    Z = cmds.getAttr('%s.translateZ' %(tmp_help[0]))
        
    cmds.move (X, Y,Z , ik_ctrl, moveXYZ = True)
    
    #cmds.parent(tmp_help, '%s:%s_leg_IK_ctrl_independent_trgt' %(ns, pfx), absolute = True)
    #r_b_leg_IK_ctrl_grp
    cmds.parent(tmp_help, '%s:%s_leg_IK_ctrl_grp' %(ns, pfx), absolute = True)
    X = cmds.getAttr('%s.rotateX' %(tmp_help[0]))
    Y = cmds.getAttr('%s.rotateY' %(tmp_help[0]))
    Z = cmds.getAttr('%s.rotateZ' %(tmp_help[0]))
    
    cmds.rotate(X, Y,Z , ik_ctrl, rotateXYZ = True)
    
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Full_IK' %(ns,pfx),0)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Roll' %(ns,pfx),0)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Clench' %(ns,pfx), (cmds.getAttr('%s:%s_leg_FK_toe_ctrl.rotateY' %(ns, pfx)) - cmds.getAttr('%s:%s_leg_settings.AnkleAngle' %(ns, pfx)))*-1)  
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Lean' %(ns,pfx), 0)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.AutoOrient' %(ns,pfx), 0)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.Twist' %(ns,pfx), 0)
    cmds.setAttr('%s:%s_leg_IK_mov_ctrl.LegTwist' %(ns,pfx), 0)
    
    cmds.delete (tmp_help)
'''
def mirror (obj, mirror = 'xy'):
    
    mirror ={}
    mirror ['xy' : 0,...]
    axis = {}
    axis ['z' : 0,...]
    
    for obj in obj:
        mrot = cmds.listAttr('%s' %obj,keyable = True, string = ('rotate%s' %(mirror[0]),'rotate%s' %(mirror[1]))) 
        rot = cmds.listAttr('%s' %obj,keyable = True, string = ('rotate%s' %(axis[0]))
        trans = cmds.listAttr('%s' %obj,keyable = True, string = ('translate%s' %(mirror[0]), 'translate%s' %(mirror[1])))
        mtrans = cmds.listAttr('%s' %obj,keyable = True, string = ('translate%s' %(axis[0])))
        
         for attr in attr:
             if
         for obj in obj:
         attr = cmds.listAttr('%s' %obj,keyable = True, string = ('rotate*', 'translate*'))
         for attr in attr:
             if
'''      
    
    
def mirrorFKleg ():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    n = getName(sel[0])
    prefix = []
    
    if n[0]=='r':
        
        prefix = 'l'
        
    else:
        
        prefix = 'r'
    
    cmds.setAttr('%s:%s%sleg_FK_shldr_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_shldr_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_shldr_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_upper_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_upper_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_upper_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_upper_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_upper_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_upper_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_lower_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_lower_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_lower_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_lower_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_lower_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_lower_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_foot_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_foot_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_foot_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_foot_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_foot_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_foot_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_toe_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_toe_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_toe_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_toe_end_jnt.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_end_jnt.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_toe_end_jnt.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_end_jnt.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_toe_end_jnt.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_toe_end_jnt.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_shldr_ctrl.Leg_Stretch' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_ctrl.Leg_Stretch' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_FK_shldr_orient_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_orient_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_shldr_orient_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_orient_ctrl.rotateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_FK_shldr_orient_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FK_shldr_orient_ctrl.rotateZ' %(ns,n[0:4])))
   
    if settings == True:
        
        mirrorLegSettings()
    
def mirrorIKleg ():

    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    n = getName(sel[0])
    prefix = []
    
    if n[0]=='r':
        
        prefix = 'l'
        
    else:
        
        prefix = 'r'
      
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.translateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.translateX' %(ns,n[0:4]))*-1)
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.translateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.translateY' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.translateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.translateZ' %(ns,n[0:4])))
     
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.rotateY' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.rotateY' %(ns,n[0:4]))*-1)
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.rotateZ' %(ns,n[0:4]))*-1)
     
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Full_IK' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Full_IK' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Pref_Knee_Shldr' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Pref_Knee_Shldr' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Follow_FK_shldr_Pos' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Follow_FK_shldr_Pos' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Follow_FK_Shldr_Ori' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Follow_FK_Shldr_Ori' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Roll' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Roll' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Clench' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Clench' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Lean' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Lean' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.AutoOrient' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.AutoOrient' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.Twist' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.Twist' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.LegTwist' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.LegTwist' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_IK_foot_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_foot_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_foot_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_foot_ctrl.rotateZ' %(ns,n[0:4])))
     
    cmds.setAttr('%s:%s%sleg_IK_mov_ctrl.rotateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_mov_ctrl.rotateX' %(ns,n[0:4])))
    cmds.setAttr('%s:%s%sleg_IK_foot_ctrl.rotateZ' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_foot_ctrl.rotateZ' %(ns,n[0:4])))
    
    cmds.setAttr('%s:%s%sleg_IK_pole_ctrl.translateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_IK_pole_ctrl.translateX' %(ns,n[0:4]))*-1)
    
    cmds.setAttr('%s:%s%sleg_FIK_shldr_pole_ctrl.translateX' %(ns,prefix,n[1:4]), cmds.getAttr('%s:%sleg_FIK_shldr_pole_ctrl.translateX' %(ns,n[0:4]))*-1)

    if settings == True:
        
        mirrorLegSettings()

def mirrorLegState ():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    prefix = []
    
    if pfx[0]=='r':
        
        prefix = 'l'
        
    else:
        
        prefix = 'r'
        
    cmds.setAttr('%s:%s%s_leg_settings.FK_IK' %(ns,prefix,pfx[1:]), cmds.getAttr('%s:%s_leg_settings.FK_IK' %(ns, pfx)))
    
    mirrorFKleg()
    mirrorIKleg()
    
    if settings == True:
        
        mirrorLegSettings()


def mirrorLegSettings():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    attr = cmds.listAttr('%s:%s_leg_settings' %(ns, pfx), keyable = True, unlocked = True, connectable = True) 
    
    if pfx[0]=='r':
        
        prefix = 'l'
        
    else:
        
        prefix = 'r'
    
    for i in range(len(attr)):
        
        cmds.setAttr('%s:%s%s_leg_settings.%s' %(ns, prefix, pfx[1:], attr[i]), cmds.getAttr('%s:%s_leg_settings.%s' %(ns, pfx, attr[i]))) 


def blendToFK ():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
        
    alignFKblend()
    cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns,pfx), 0)
    cmds.select('%s:%s_leg_FK_shldr_ctrl' %(ns,pfx))
    print 'switched to FK'

        
def blendToIK ():
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    
    alignIKleg()
    cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns,pfx), 10)
    cmds.select('%s:%s_leg_IK_mov_ctrl' %(ns,pfx))
    print 'switched to IK'


def SwitchMode (fk = True, align = True):
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    
    if fk == True and align == True:
        
        blendToFK()
        
    elif fk == True and align == False:
    
        cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns,pfx), 0)
        
    elif fk == False and align == True:
    
        blendToIK()
        
    elif fk == False and align == False:
        
        cmds.setAttr('%s:%s_leg_settings.FK_IK' %(ns,pfx), 10) 
    

def pfxSel (obj):
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    pfx = getPrefix(sel[0])
    
    cmds.select(sel, deselect = True)
    
    for i in obj:
        
        print i    
        cmds.select('%s:%s_%s' %(ns, pfx, i), add = True)
        

def sel (obj):
    
    sel = cmds.ls (selection = True)
    ns = getNamespace(sel[0])
    
    cmds.select(sel, deselect = True)
    
    for i in obj:
        
        print i    
        cmds.select('%s:%s' %(ns, i), add = True)
    